part of 'page.dart';

class _LearningSettingsPageWloc extends StatefulWloc<_LearningSettingsPageWlocState, LearningSettingsPage> {
  late final bloc.LearningSettingsPageBloc _bloc;

  @override
  _LearningSettingsPageWlocState get initialState {
    return _LearningSettingsPageWlocStateFactory().create(blocState: _bloc.state,);
  }

  void onAutoPronunciationEnablementChange() {
    _bloc.requestAutoPronunciationEnablementToggling();
  }

  void onForgottenLexicalItemTrainingEnablementChange() {
    _bloc.requestForgottenLexicalItemTrainingEnablementToggling();
  }

  void onExerciseShuffleEnablementChange() {
    _bloc.requestExerciseShuffleEnablementToggling();
  }

  void onTrainingLexicalItemMaxCountTilePressed() async {
    _bloc.requestTrainingLexicalItemMaxCountChangeStart();
  }

  Function(bool)? buildOnTranslationSelectionExerciseEnablementChangeCallback() {
    if (state.translationSelectionExerciseDisablementDisallowed) {
      return null;
    }

    return (_) {
      _bloc.requestTranslationSelectionExerciseEnablementToggling();
    };
  }

  Function(bool)? buildOnAudioExerciseEnablementChangeCallback() {
    if (state.audioExerciseDisablementDisallowed) {
      return null;
    }

    return (_) {
      _bloc.requestAudioExerciseEnablementToggling();
    };
  }

  Function(bool)? buildOnTypingExerciseEnablementChangeCallback() {
    if (state.typingExerciseDisablementDisallowed) {
      return null;
    }

    return (_) {
      _bloc.requestTypingExerciseEnablementToggling();
    };
  }

  void _showTrainingLexicalItemMaxCountChangeDialog() async {
    final childCount =
        state.trainingLexicalItemMaxCountUpperLimit - state.trainingLexicalItemMaxCountLowerLimit + 1;

    final scrollController = FixedExtentScrollController(
      initialItem: state.trainingLexicalItemMaxCount -
          state.trainingLexicalItemMaxCountLowerLimit,
    );

    await showCupertinoModalPopup(
      context: context,
      builder: (context) {
        return Container(
          height: 216,
          color: HomeCoreTheme.of(context).cardColor,
          child: CupertinoPicker.builder(
            itemExtent: 50,
            scrollController: scrollController,
            childCount: childCount,
            onSelectedItemChanged: (index) {
              _bloc.requestTrainingLexicalItemMaxCountChange(
                trainingLexicalItemMaxCount:
                index + state.trainingLexicalItemMaxCountLowerLimit,
              );
            },
            itemBuilder: (context, index) {
              return Center(
                child: Text('${index + state.trainingLexicalItemMaxCountLowerLimit}'),
              );
            },
          ),
        );
      },
    );

    scrollController.dispose();
  }

  void _onBlocStateChanged(bloc.LearningSettingsPageBlocState blocState) {
    final updatedState = _LearningSettingsPageWlocStateFactory().create(blocState: blocState,);

    emit(updatedState);

if (blocState.trainingLexicalItemMaxCountChangeDialogShown) {
  _showTrainingLexicalItemMaxCountChangeDialog();
}
  }

  @override
  void initState() {
    super.initState();

    _bloc = bloc.LearningSettingsPageBloc(
      userInfoBloc: core_di.sl(),
      settingsBloc: core_di.sl(),
      purchaseFlowDisplayBloc: core_di.sl(),
      getInitialData: learning_settings_page_di.sl(),
      changeTrainingLexicalItemMaxCount: learning_settings_page_di.sl(),
      handleLearningSettingsOuterUpdate: learning_settings_page_di.sl(),
      handlePremiumAccessStatusOuterUpdate: learning_settings_page_di.sl(),
      toggleAudioExerciseEnablement: learning_settings_page_di.sl(),
      toggleAutoPronunciationEnablement: learning_settings_page_di.sl(),
      toggleExerciseShuffleEnablement: learning_settings_page_di.sl(),
      toggleForgottenLexicalItemTrainingEnablement: learning_settings_page_di.sl(),
      toggleTranslationSelectionExerciseEnablement: learning_settings_page_di.sl(),
      toggleTypingExerciseEnablement: learning_settings_page_di.sl(),
    );

    _bloc.stream.listen(_onBlocStateChanged);
  }

  @override
  void dispose() {
    _bloc.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _LearningSettingsPageView(
      wloc: this,
    );
  }
}
